let redis = require('../dao/redis');
let redisConst = require('../const/redisConst')

class WxService {
    constructor() {
    }
    //微信access_token的缓存
    async getWxCodeCache(code) {
        let info=await redis.getHashItem(redisConst.wxCodeCacheMap,code);
        console.logw('getWxCodeCache ',info);
        if(info==null) return null;
        let data=null;
        try {
            data=JSON.parse(info);
            let  time=(new Date().getTime()-data.tick)/1000;
            if(time>data.expires_in) {
                console.logw('getWxCodeCache 超过有效时间了请重新拉取');
                return null;
            }
        } catch(e) {
            return null;
        }
        return data;
    }

    async cacheLoginInfo(uid, info) {
        let s = JSON.stringify(info);
        await redis.setHashItem(redisConst.wechatCacheMap, uid, s);
        await redis.setHashItem(redisConst.wechatCacheMap, info.account, s);
    }
}

let wxService = new WxService();
module.exports = wxService;
